package com.ljh.onlinetest.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljh.onlinetest.domain.ExamPaperQuestionCustomerAnswer;
import com.ljh.onlinetest.domain.TextContent;
import com.ljh.onlinetest.domain.enums.QuestionTypeEnum;
import com.ljh.onlinetest.domain.other.ExamPaperAnswerUpdate;
import com.ljh.onlinetest.domain.other.KeyValue;
import com.ljh.onlinetest.repository.BaseMapper;
import com.ljh.onlinetest.repository.ExamPaperQuestionCustomerAnswerMapper;
import com.ljh.onlinetest.service.ExamPaperQuestionCustomerAnswerService;
import com.ljh.onlinetest.service.TextContentService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.utils.ExamUtil;
import com.ljh.onlinetest.utils.JsonUtil;
import com.ljh.onlinetest.viewmodel.student.exam.ExamPaperSubmitItemVM;
import com.ljh.onlinetest.viewmodel.student.question.answer.QuestionPageStudentRequestVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 余一
 * @date 2022/2/22  23:33
 */
@Service
public class ExamPaperQuestionCustomerAnswerServiceImpl extends BaseServiceImpl<ExamPaperQuestionCustomerAnswer> implements ExamPaperQuestionCustomerAnswerService {

    private final ExamPaperQuestionCustomerAnswerMapper answerMapper;
    private final TextContentService textContentService;

    @Autowired
    public ExamPaperQuestionCustomerAnswerServiceImpl(ExamPaperQuestionCustomerAnswerMapper answerMapper,TextContentService textContentService) {
        super(answerMapper);
        this.answerMapper = answerMapper;
        this.textContentService = textContentService;
    }

    @Override
    public Integer selectAllCount() {
        return answerMapper.selectAllCount();
    }

    @Override
    public List<Integer> selectMothCount() {
        Date startTime = DateTimeUtil.getMonthStartDay();
        Date endTime = DateTimeUtil.getMonthEndDay();
        List<KeyValue> mouthCount = answerMapper.selectCountByDate(startTime, endTime);
        List<String> mothStartToNowFormat = DateTimeUtil.MothStartToNowFormat();
        return mothStartToNowFormat.stream().map(md -> {
            KeyValue keyValue = mouthCount.stream().filter(kv -> kv.getName().equals(md)).findAny().orElse(null);
            return null == keyValue ? 0 : keyValue.getValue();
        }).collect(Collectors.toList());
    }

    @Override
    public List<ExamPaperQuestionCustomerAnswer> selectListByPaperAnswerId(Integer id) {
        return answerMapper.selectListByPaperAnswerId(id);
    }

    @Override
    public ExamPaperSubmitItemVM examPaperQuestionCustomerAnswerToVM(ExamPaperQuestionCustomerAnswer a) {
        ExamPaperSubmitItemVM vm = new ExamPaperSubmitItemVM();
        vm.setId(a.getId());
        vm.setQuestionId(a.getQuestionId());
        vm.setDoRight(a.getDoRight());
        vm.setItemOrder(a.getItemOrder());
        vm.setQuestionScore(ExamUtil.scoreToVM(a.getQuestionScore()));
        vm.setScore(ExamUtil.scoreToVM(a.getCustomerScore()));
        //根据题目类型填充答案的内容
        setSpecialToVM(vm,a);
        return vm;
    }

    @Override
    public int updateScore(List<ExamPaperAnswerUpdate> examPaperAnswerUpdates) {
        return answerMapper.updateScore(examPaperAnswerUpdates);
    }

    @Override
    public void insertList(List<ExamPaperQuestionCustomerAnswer> examPaperQuestionCustomerAnswers) {
        answerMapper.insertList(examPaperQuestionCustomerAnswers);
    }

    @Override
    public PageInfo<ExamPaperQuestionCustomerAnswer> studentPage(QuestionPageStudentRequestVM model) {
        return PageHelper.startPage(model.getPageIndex(),model.getPageSize(),"id desc").doSelectPageInfo(() ->
                    answerMapper.studentPage(model)
                 );
    }

    private void setSpecialToVM(ExamPaperSubmitItemVM vm, ExamPaperQuestionCustomerAnswer a) {
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.fromCode(a.getQuestionType());
        switch (questionTypeEnum){
            case MultipleChoice:
                vm.setContent(a.getAnswer());
                vm.setContentArray(ExamUtil.contentToArray(a.getAnswer()));
                break;
            case GapFilling:
                TextContent textContent = textContentService.selectById(a.getTextContentId());
                List<String> correctAnswer = JsonUtil.toJsonListObject(textContent.getContent(), String.class);
                vm.setContentArray(correctAnswer);
                break;
            default:
                if (QuestionTypeEnum.needSaveTextContent(a.getTextContentId())){
                    TextContent content = textContentService.selectById(a.getTextContentId());
                    vm.setContent(content.getContent());
                } else {
                    vm.setContent(a.getAnswer());
                }
                break;
        }
    }
}
